package net.oschina.app.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import net.oschina.app.R;
import net.oschina.app.adapter.TabVPAdpater;

import butterknife.BindView;
import butterknife.ButterKnife;

/*1. 综合模块滑动界面实现步骤
        * 1. 初始化布局, 将包含页面滑动标签(PageSlidngTabStrip\TabLayout),ViewPager的布局初始化

        （TabLayout\PageSlidngTabStrip)+ViewPager+Fragment
        * 2. 查找关心的控件
        TabLayout
        ViewPager
        * 3. 创建ViewPager数据适配器   PagerAdapter
        FragmentPagerAdapter--> FragmentStatePagerAdapter
        * 4. 初始化页签信息
        : 标签标题   标签所对应的Fragment   传递给Fragment的参数
        * 5. 将数据适配器设置给ViewPager
        * 6. 页签布局(TabLayout)要和ViewPager要绑定/关联*/
public abstract class BaseTabVPFragment extends Fragment {
    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.pager)
    ViewPager viewPager;
    private TabVPAdpater adpater;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        /********************  与界面UI相关 **************************/

        //1. 初始化布局, 将包含页面滑动标签(PageSlidngTabStrip\TabLayout),ViewPager的布局初始化
        View view = inflater.inflate(R.layout.base_viewpage_fragment, container, false);
        //2. 绑定控件
        ButterKnife.bind(this,view);

        //3.创建ViewPager数据适配器   PagerAdapter
        adpater = new TabVPAdpater(getContext(),getChildFragmentManager());

        //4. 将数据适配器设置给ViewPager
        viewPager.setAdapter(adpater);
        //5. 页签布局(TabLayout)要和ViewPager要绑定/关联
        tabLayout.setupWithViewPager(viewPager);

     /********************  与数据相关 **************************/
        //6. 初始化页签信息 :与数据相关 :页签的标题  ： 页签对应的片段  ，向片段传递的参数
        setupTab(adpater);

        return view;
    }
    //取得Bundle
    public Bundle getBundle(String arg) {
        Bundle bundle=new Bundle();
        bundle.putString("key",arg);
        return  bundle;
    }

    /**初始化页签的方法
	 * 页签的标题
     * 页签对应的片段
     * 向片段传递的参数
     */
/*    private void setupTab() {
        adpater.addTab("资讯2",DefaultFragment.class,getBundle("你的资讯"));
        adpater.addTab("热点2",DefaultFragment.class,getBundle("你的热点"));
        adpater.addTab("博客2",DefaultFragment.class,getBundle("你的博客"));
        adpater.addTab("推荐2",DefaultFragment.class,getBundle("你的推荐"));*//*
    }*/
    //由于基类不能决定子类中的标签设置 ，所以抽象出去，由子类来实现
    public  abstract  void setupTab(TabVPAdpater adpater);
}
